<?php


namespace App\Http\Model\Console;


use App\Enum\EnumEnum;
use Illuminate\Support\Facades\DB;

class EnumModel
{

    /**
     * 刷新班级数据
     *
     * @param $pfsId
     * @param $createDate
     *
     * @return int
     */
    public static function className($pfsId, $createDate)
    {
        $list = DB::select('SELECT `class_name`,`class_id` FROM `class_list_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `is_del`=0 AND `create_date`>:create_date GROUP BY `class_id`', [
            'pfs_dw_id'   => $pfsId,
            'create_date' => $createDate,
        ]);
        if (empty($list)) {
            return 0;
        }
        foreach ($list as $val) {
            $exist = DB::select('SELECT `id` FROM `enum` WHERE `pfs_dw_id`=:pfs_dw_id AND `classify`=:classify AND `key`=:key AND `is_del`=0', [
                'pfs_dw_id' => $pfsId,
                'classify'  => EnumEnum::CLASS_NAME,
                'key'       => $val->class_id,
            ]);
            if (empty($exist)) {
                DB::table('enum')->insert([
                    'pfs_dw_id' => $pfsId,
                    'classify'  => EnumEnum::CLASS_NAME,
                    'key'       => $val->class_id,
                    'val'       => $val->class_name,
                    'desc'      => '班级信息'
                ]);
            }
        }
    }


    /**
     * 刷新主体信息
     *
     * @param $pfsId
     * @param $createDate
     *
     * @return int
     */
    public static function ledgerSubject($pfsId, $createDate)
    {
        $list = DB::select('SELECT `ledger_subject_name`,`ledger_subject_id` FROM `class_list_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `is_del`=0 AND `create_date`>:create_date GROUP BY `ledger_subject_name`', [
            'pfs_dw_id'   => $pfsId,
            'create_date' => $createDate,
        ]);
        if (empty($list)) {
            return 0;
        }
        foreach ($list as $val) {
            $exist = DB::select('SELECT `id` FROM `enum` WHERE `pfs_dw_id`=:pfs_dw_id AND `classify`=:classify AND `val`=:val AND `is_del`=0', [
                'pfs_dw_id' => $pfsId,
                'classify'  => EnumEnum::LEDGER_SUBJECT,
                'val'       => $val->ledger_subject_name,
            ]);
            if (empty($exist)) {
                DB::table('enum')->insert([
                    'pfs_dw_id' => $pfsId,
                    'classify'  => EnumEnum::LEDGER_SUBJECT,
                    'key'       => $val->ledger_subject_id,
                    'val'       => $val->ledger_subject_name,
                    'desc'      => '主体信息'
                ]);
            }
        }
    }


    /**
     * 刷新城市信息
     *
     * @param $pfsId
     * @param $createDate
     *
     * @return int
     */
    public static function city($pfsId, $createDate)
    {
        $list = DB::select('SELECT `city_name`,`area_code` FROM `class_list_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `is_del`=0 AND `create_date`>:create_date GROUP BY `city_name`', [
            'pfs_dw_id'   => $pfsId,
            'create_date' => $createDate,
        ]);
        if (empty($list)) {
            return 0;
        }
        foreach ($list as $val) {
            $exist = DB::select('SELECT `id` FROM `enum` WHERE `pfs_dw_id`=:pfs_dw_id AND `classify`=:classify AND `val`=:val AND `is_del`=0', [
                'pfs_dw_id' => $pfsId,
                'classify'  => EnumEnum::CITY,
                'val'       => $val->city_name,
            ]);
            if (empty($exist)) {
                DB::table('enum')->insert([
                    'pfs_dw_id' => $pfsId,
                    'classify'  => EnumEnum::CITY,
                    'key'       => $val->area_code,
                    'val'       => $val->city_name,
                    'desc'      => '城市信息'
                ]);
            }
        }
    }


    /**
     * 监管账户列表
     *
     * @return array
     */
    public static function pfs()
    {
        return DB::select('SELECT `id` FROM `pfs_dw_ini` WHERE `is_del`=0');
    }

}





/**
 * EnumModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/15 10:36:00   操作:创建
 **/
